﻿function OdataParam(obj) {
    this.url = obj.url;
    this.pageroll = 10;
    this.index = isNaN(obj.index) ? 1 : obj.index;
    this.maxcount = 0;
    this.filter = '';
    this.orderby = '';

    if (!isNaN(obj.pageroll) && obj.pageroll > 0) {
        this.pageroll = obj.pageroll
    }

    if (!isNaN(obj.index) && obj.index > 0) {
        this.index = obj.index
    }

    this.maxpage = function () {
        if (this.maxcount % this.pageroll == 0) {
            return (this.maxcount / this.pageroll) > 0 ? (this.maxcount / this.pageroll) : 1;
        }

        return ((this.maxcount / this.pageroll) + 1);
    }

    this.urlFormat = function () {
        var u = this.url;

        if (isNaN(this.pageroll)) {
            this.pageroll = 20;
        }

        u = u + "&$top=" + this.pageroll;

        if (this.index > this.maxpage()) {
            this.index = this.maxpage()
        }

        if (this.index > 1) {
            u = u + "&$skip=" + ((this.index - 1) * this.pageroll);
        }

        if (this.filter.length > 0) {
            u = u + "&$filter=" + this.filter
        }

        if (this.orderby.length > 0) {
            u = u + "&$orderby=" + this.orderby;
        }

        return u;
    }

    this.pageControl = function () {
        var html = '<div class="row"><div class="col-md-6"><ul class="pagination">';

        html = html + '<li><a href="javascript:GoPage(1)">首页</a></li>';

        for (var i = this.index - 5; i < this.index; i++) {
            if (i > 0) {
                html = html + '<li><a href="javascript:GoPage(' + i + ')">' + i + '</a></li>';
            }
        }

        html = html + '<li class="active"><a href="javascript:void(0)">' + this.index + '</a></li>';

        for (var i = this.index + 1; i < this.index + 6; i++) {
            if (i <= this.maxpage()) {
                html = html + '<li><a href="javascript:GoPage(' + i + ')">' + i + '</a></li>';
            }
        }

        html = html + '<li><a href="javascript:GoPage(' + this.maxpage() + ')">末页</a></li>';

        html = html + '</ul></div><div class="col-md-6">';

        html = html + '<div class="input-group" style="margin:20px 0px 20px 0px;"><span class="input-group-addon" >每页</span ><input type="text" class="form-control" value="' + this.pageroll + '" id="OdataParamPageRoll"><span class="input-group-addon" >条</span ><span class="input-group-addon" >第</span ><input type="text" class="form-control" value="' + this.index + '" id="OdataParamIndex"><span class="input-group-addon" >页</span><span class="input-group-btn"><button type="button" class="btn btn-info btn-flat" onclick="OdataParamSetPage(\'#OdataParamPageRoll\',\'#OdataParamIndex\')">转到</button></span></div></div></div>'

        return html;
    }
}